package com.jame.basics.thread.concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * @author : Jame
 * @date : 2021-01-13 13:45
 **/
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main线程开始执行");
        /**
         * CountDownLatch可以用来等待其他线程完毕后,再执行下面代码
         */
        //设置初始大小为3
        CountDownLatch countDownLatch=new CountDownLatch(5);
        for (int i = 0; i < 3; i++) {
            new  Thread(()->{
                //将计数器减一
                countDownLatch.countDown();
                System.out.println(Thread.currentThread().getName()+"执行完毕");
            },String.valueOf(i)).start();
        }
        //抛异常,当计数器为0时执行下面代码
        countDownLatch.await();
        System.out.println("main线程执行完毕");

    }
}
